import java.util.Properties;

import edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation;
import edu.stanford.nlp.neural.rnn.RNNCoreAnnotations;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.sentiment.SentimentCoreAnnotations.AnnotatedTree;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.util.CoreMap;


public class SentimentAnalysis {
	
	private StanfordCoreNLP pipeline;
	
	public SentimentAnalysis() {
		Properties props = new Properties();
	    props.put("annotators", "tokenize, ssplit, parse, sentiment");
	    pipeline = new StanfordCoreNLP(props);
	}

	/*
	 * 0 = very negative, 1 = negative, 2 = neutral, 3 = positive,
	 * and 4 = very positive
	 */
	public Integer getSentiment(String line) {
		if (line == null || line.length() == 0)
			return 2;
		
	    Annotation ano = pipeline.process(line);
	    
	    for (CoreMap sentence : ano.get(SentencesAnnotation.class)) {
	    	Tree tree = sentence.get(AnnotatedTree.class);
	    	return RNNCoreAnnotations.getPredictedClass(tree);
	    }
	    
	    return 2;
	}
	
}
